package com.admin.service.impl;

import com.admin.persistence.sys.entity.Person;
import com.admin.persistence.sys.mapper.PersonMapper;
import com.admin.service.SysPersonService;
import com.admin.service.SysUserGroupService;
import com.admin.service.SysUserService;
import com.admin.service.vo.SysPerson;
import com.admin.service.vo.SysUser;
import com.admin.service.vo.SysUserGroup;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.admin.common.CommonUtils;
import com.admin.common.biz.BizBaseResult;
import com.admin.common.biz.ModelUseStatus;
import com.admin.common.biz.ResultStatus;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Service
public class SysPersonServiceImp implements SysPersonService {

    @Autowired
    private PersonMapper personMapper;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysUserGroupService sysUserGroupService;


    @Transactional
    @Override
    public SysPerson createSysPerson(SysPerson sysPerson) {
        List<String> userIds = new ArrayList<>(), userGroupIds = new ArrayList<>(), personIds = new ArrayList<>();
        Person person = sysPerson.getPerson();
        person.setId(CommonUtils.createId());
        if (saveSysPerson(sysPerson) < 1) return null;
        personIds.add(person.getId());


        //获取用户Ids 并绑定 用户
        if (!CollectionUtils.isEmpty(sysPerson.getUsers())) {
            userIds = sysPerson.getUsers().stream().map(cur -> {
                return cur.getUser().getId();
            }).collect(Collectors.toList());
            userIds = sysUserService.bindPerson(new ArrayList<String>(), userIds);
        }

        //获取用户组Ids 并绑定用户组
        if (!CollectionUtils.isEmpty(userIds) && !CollectionUtils.isEmpty(sysPerson.getUserGroups())) {
            userGroupIds = sysPerson.getUserGroups().stream().map(cur -> {
                return cur.getUserGroup().getId();
            }).collect(Collectors.toList());
            sysUserGroupService.bindUser(userIds, userGroupIds);
        }
        return sysPerson;
    }

    @Transactional
    @Override
    public int saveSysPerson(SysPerson sysPerson) {
        if (queryById(sysPerson.getPerson().getId()) != null) {
            return personMapper.updateById(sysPerson.getPerson());
        } else {
            sysPerson.getPerson().setId(CommonUtils.createId());
            return personMapper.insert(sysPerson.getPerson());
        }
    }

    @Transactional
    @Override
    public int edtionSysPerson(SysPerson sysPerson) {
        return personMapper.updateById(sysPerson.getPerson());
    }

    @Transactional
    @Override
    public int remvoSysPerson(SysPerson sysPerson) {
        List<String> personIds = new ArrayList<String>();
        personIds.add(sysPerson.getPerson().getId());
        sysUserGroupService.unbindUser(sysUserService.unbindPerson(personIds));
        sysPerson.getPerson().setStatus(ModelUseStatus.REMVOE.value);
        return personMapper.updateById(sysPerson.getPerson());
    }

    @Override
    public List<SysPerson> queryAll() {
        List<SysPerson> sysPersonList = new ArrayList<SysPerson>();
        List<Person> personList = personMapper.selectList(null);
        if (!CollectionUtils.isEmpty(personList)) {
            personList.stream().forEach(cur -> {
                List<SysUser> sysUsers = sysUserService.queryListByPersonInfo(cur.getId(), null, null, null, null, null, true);
                List<SysUserGroup> sysUserGroups = sysUserGroupService.queryByPersonInfo(cur.getId(), null, null, null, null,
                        null, true);
                sysPersonList.add(new SysPerson(cur, sysUsers, sysUserGroups));
            });
        }
        return sysPersonList;
    }

    @Override
    public List<SysPerson> queryByPersonInfo(String idCard, String phone, String email) {
        List<SysPerson> sysPersonList = new ArrayList<SysPerson>();
        QueryWrapper<Person> queryWrapper = new QueryWrapper<Person>();
        if (!StringUtils.isEmpty(idCard)) {
            queryWrapper.or().eq("ID_CARD", idCard);
        }
        if (!StringUtils.isEmpty(phone)) {
            queryWrapper.or().eq("PHONE", phone);
        }
        if (!StringUtils.isEmpty(email)) {
            queryWrapper.or().eq("EMIAL", email);
        }
        List<Person> people = personMapper.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(people)) {
            people.stream().forEach(cur -> {
                sysPersonList.add(new SysPerson(cur,
                        sysUserService.queryListByPersonInfo(cur.getId(), null, null, null, null, null, true),
                        sysUserGroupService.queryByPersonInfo(cur.getId(), null, null, null, null, null,
                                true)));
            });
        }
        return sysPersonList;
    }

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    @Override
    public SysPerson queryById(String id) {
        Person person = personMapper.selectById(id);
        if (person == null) return null;
        List<SysUser> sysUsers = sysUserService.queryListByPersonInfo(person.getId(), null, null, null, null, null, true);
        List<SysUserGroup> sysUserGroups = sysUserGroupService.queryByPersonInfo(person.getId(), null, null, null, null, null,
                true);
        return new SysPerson(person, sysUsers, sysUserGroups);
    }

    /**
     * 能否删除
     *
     * @param sysPerson
     * @return
     */
    public BizBaseResult canRomevo(SysPerson sysPerson) {
        BizBaseResult bizBaseResult = new BizBaseResult();
        Person person = sysPerson.getPerson();

        List<SysUser> sysUsers = sysUserService.queryListByPersonInfo(person.getId(), null, null, null, null, null, true);
        if (!CollectionUtils.isEmpty(sysUsers)) {
            bizBaseResult.setStatus(ResultStatus.FAIL.value);
            bizBaseResult.setResultStr("已挂勾用户");
            return bizBaseResult;
        }
        List<SysUserGroup> sysUserGroups = sysUserGroupService.queryByPersonInfo(person.getId(), null, null, null, null,
                null, true);

        if (!CollectionUtils.isEmpty(sysUsers)) {
            bizBaseResult.setStatus(ResultStatus.FAIL.value);
            bizBaseResult.setResultStr("已挂勾用户组");
            return bizBaseResult;
        }
        return bizBaseResult;
    }

}
